package com.it.day7;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicMarkableReference;

/**
 * ClassName: AtomicMarkableReferenceDemo
 * Description: 引用类型原子类
 *
 * @Author: csx
 * @Create: 2025/5/13 - 9:47
 * @version: v1.0
 */
public class AtomicMarkableReferenceDemo {
    static AtomicMarkableReference<Integer> amr = new AtomicMarkableReference<>(100, false);

    public static void main(String[] args) {
        new Thread(() -> {
            boolean marked = amr.isMarked();
            System.out.println(Thread.currentThread().getName() + "\t-----默认标识位：" + marked);
            try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
            amr.compareAndSet(100,101,marked,!marked);
        }, "t1").start();

        new Thread(() -> {
            boolean marked = amr.isMarked();
            System.out.println(Thread.currentThread().getName() + "\t-----默认标识位：" + marked);
            try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
            boolean result = amr.compareAndSet(100, 2025, marked, !marked);
            System.out.println(Thread.currentThread().getName() + "\t操作是否成功：" + result);
            System.out.println(Thread.currentThread().getName() + "\t最终值：" + amr.getReference());
            System.out.println(Thread.currentThread().getName() + "\t当前标志位：" + amr.isMarked());
        }, "t2").start();
    }
}











